/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introsemp;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.Calendar;
import java.awt.Toolkit;
/**
 *
 * @author G
 */
public class TimerComponent {
    
    
    protected int computerNo=0;
    public int hour=0;
    public int minute=0;
    public int second=0;
    public float timelasped=0;
    public float rates=0;
    public float price=0;
    public float serviceprice=0;
    private int   m_interval  = 1000;  // Milliseconds between updates.
    private Timer m_timer;
    private javax.swing.JTable terminalsTable;
    public boolean isFixed=false;
    public boolean inSession=false;
    public boolean isPaused=false;
    public String timeStarted="";
    public int Sethour=0;
    public int Setminute=0;
    Form_MainMenu menu;
    
    public TimerComponent(javax.swing.JTable TerminalsTableGet,int number,Form_MainMenu menuGet){
    m_timer = new Timer(m_interval, new TimerAction());
    terminalsTable=TerminalsTableGet;
    computerNo=number-1;
    menu=menuGet;
    }
      class TimerAction implements ActionListener {
     
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!isFixed){
            second++;
            timelasped ++;
            price = (timelasped/3600)*rates;
            price=(price-price%1)+1;
            String priceSt= Float.toString(price+serviceprice);
            if((Integer)menu.spinner.getValue()==computerNo+1)menu.total.setText(priceSt);
            terminalsTable.setValueAt(price,computerNo,2);
                if(second>=60){
                second=0;
                minute++;
                }
                if(minute>=60){
                minute=0;
                hour++;
                }
                
            if(hour>9 && minute>9 && second>9){
            terminalsTable.setValueAt(hour+":"+minute+":"+second,computerNo,1);
            }
            else if(hour<=9 && minute<=9 && second>9){
            terminalsTable.setValueAt("0"+hour+":0"+minute+":"+second,computerNo,1);
            }
            else if(hour>9 && minute<=9 && second>9){
            terminalsTable.setValueAt(hour+":0"+minute+":"+second,computerNo,1);
            }
            else if(hour<=9 && minute>9 && second>9){
            terminalsTable.setValueAt("0"+hour+":"+minute+":"+second,computerNo,1);
            }
            
            else if(hour>9 && minute>9 && second<=9){
            terminalsTable.setValueAt(hour+":"+minute+":0"+second,computerNo,1);
            }
            else if(hour<=9 && minute<=9 && second<=9){
            terminalsTable.setValueAt("0"+hour+":0"+minute+":0"+second,computerNo,1);
            }
            else if(hour>9 && minute<=9 && second<=9){
            terminalsTable.setValueAt(hour+":0"+minute+":0"+second,computerNo,1);
            }
            else if(hour<=9 && minute>9 && second<=9){
            terminalsTable.setValueAt("0"+hour+":"+minute+":0"+second,computerNo,1);
            }
                        
            } 
        else{
            timelasped++;
            price = (timelasped/3600)*rates;
            price=(price-price%1)+1;
            terminalsTable.setValueAt(price,computerNo,2);
            String priceSt= Float.toString(price+serviceprice);
           if((Integer)menu.spinner.getValue()==computerNo+1) menu.total.setText(priceSt);
           if(second<3 && minute==0 && hour==0){
                    Toolkit.getDefaultToolkit().beep();
            }
           if(second==0 && minute==0 && hour==0){
                    m_timer.stop();               
            }
           
           else{
               second--;
            if(second<0){
               second=59;
               minute--;
           }
           if(minute<0){
               minute=59;
               hour--;
           }   
      }
            if(hour>9 && minute>9 && second>9){
            terminalsTable.setValueAt(hour+":"+minute+":"+second,computerNo,1);
            }
            else if(hour<=9 && minute<=9 && second>9){
            terminalsTable.setValueAt("0"+hour+":0"+minute+":"+second,computerNo,1);
            }
            else if(hour>9 && minute<=9 && second>9){
            terminalsTable.setValueAt(hour+":0"+minute+":"+second,computerNo,1);
            }
            else if(hour<=9 && minute>9 && second>9){
            terminalsTable.setValueAt("0"+hour+":"+minute+":"+second,computerNo,1);
            }
            
            else if(hour>9 && minute>9 && second<=9){
            terminalsTable.setValueAt(hour+":"+minute+":0"+second,computerNo,1);
            }
            else if(hour<=9 && minute<=9 && second<=9){
            terminalsTable.setValueAt("0"+hour+":0"+minute+":0"+second,computerNo,1);
            }
            else if(hour>9 && minute<=9 && second<=9){
            terminalsTable.setValueAt(hour+":0"+minute+":0"+second,computerNo,1);
            }
            else if(hour<=9 && minute>9 && second<=9){
            terminalsTable.setValueAt("0"+hour+":"+minute+":0"+second,computerNo,1);
            }
            
            
            
      
        
    }
    }
      }
       public void setTimer(boolean turnOnOff) {
        if (turnOnOff) {
            m_timer.start();  // start animation by starting the timer.
        } else {
            m_timer.stop();   // stop timer
        }
    }
       
       public void setFixedTime(int hourGet, int minuteGet){
           isFixed=true;
           hour=hourGet;
           minute=minuteGet;
           m_timer.start();
           Sethour=hourGet;
           Setminute=minuteGet;
       }
       
       public void resetTimer(){
        m_timer.stop();
        hour=0;
        minute=0;
        second=0;
        timelasped=0;
        price=0;
        isFixed=false;
        inSession=false;
        isPaused=false;
        timeStarted="";
        serviceprice=0;
        Sethour=0;
        Setminute=0;
        terminalsTable.setValueAt("Available",computerNo,1);
        terminalsTable.setValueAt(price,computerNo,2);
       }
       
       public void disable(){
       m_timer.stop();
        hour=0;
        minute=0;
        second=0;
        timelasped=0;
        price=0;
        isFixed=false;
        inSession=false;
        isPaused=false;
        timeStarted="";
        serviceprice=0;
        Sethour=0;
        Setminute=0;
        terminalsTable.setValueAt("Disabled",computerNo,1); 
        terminalsTable.setValueAt(price,computerNo,2);
       }
       
       public void enable(){
       terminalsTable.setValueAt("Available",computerNo,1);
       }
    
}
